using MaterialSkin.Controls;
using System;
using System.Drawing;
using System.Windows.Forms;
using VideoAutoGenerator.Core;

namespace VideoAutoGenerator.UI.UserControls
{
    public partial class ProjectSettingsControl : MaterialSkin.Controls.MaterialCard
    {
        // 项目设置控件
        private MaterialLabel imageDurationLabel;
        private NumericUpDown imageDurationNumeric;
        
        private MaterialLabel defaultAudioDurationLabel;
        private NumericUpDown defaultAudioDurationNumeric;
        
        private MaterialLabel videoWidthLabel;
        private NumericUpDown videoWidthNumeric;
        
        private MaterialLabel videoHeightLabel;
        private NumericUpDown videoHeightNumeric;
        
        private MaterialLabel useTransitionEffectLabel;
        private MaterialCheckbox useTransitionEffectCheckBox;
        
        public ProjectSettingsControl()
        {
            InitializeComponent();
        }
        
        private void InitializeComponent()
        {
            this.SuspendLayout();
            
            // 创建控件
            CreateControls();
            
            // 设置控件位置和样式
            SetupLayout();
            
            this.ResumeLayout(false);
        }
        
        private void CreateControls()
        {
            // 图片显示时长
            imageDurationLabel = new MaterialLabel();
            imageDurationNumeric = new NumericUpDown();
            
            // 默认音频时长
            defaultAudioDurationLabel = new MaterialLabel();
            defaultAudioDurationNumeric = new NumericUpDown();
            
            // 视频宽度
            videoWidthLabel = new MaterialLabel();
            videoWidthNumeric = new NumericUpDown();
            
            // 视频高度
            videoHeightLabel = new MaterialLabel();
            videoHeightNumeric = new NumericUpDown();
            
            // 使用过渡效果
            useTransitionEffectLabel = new MaterialLabel();
            useTransitionEffectCheckBox = new MaterialCheckbox();
        }
        
        private void SetupLayout()
        {
            // 设置控件属性
            this.AutoScroll = true;
            this.Dock = DockStyle.Fill;
            
            // 图片显示时长
            imageDurationLabel.Text = "图片显示时长毫秒:";
            imageDurationLabel.AutoSize = true; // 允许标签自动调整大小以适应文本
            imageDurationNumeric.Minimum = 100;
            imageDurationNumeric.Maximum = 10000;
            imageDurationNumeric.Value = 3000;
            imageDurationNumeric.DecimalPlaces = 0;
            
            // 默认音频时长
            defaultAudioDurationLabel.Text = "默认音频时长(毫秒):";
            defaultAudioDurationLabel.AutoSize = true; // 允许标签自动调整大小以适应文本
            defaultAudioDurationNumeric.Minimum = 1000;
            defaultAudioDurationNumeric.Maximum = 300000;
            defaultAudioDurationNumeric.Value = 60000;
            defaultAudioDurationNumeric.DecimalPlaces = 0;
            
            // 视频宽度
            videoWidthLabel.Text = "视频宽度(像素):";
            videoWidthLabel.AutoSize = true; // 允许标签自动调整大小以适应文本
            videoWidthNumeric.Minimum = 320;
            videoWidthNumeric.Maximum = 3840;
            videoWidthNumeric.Value = 1080;
            videoWidthNumeric.DecimalPlaces = 0;
            
            // 视频高度
            videoHeightLabel.Text = "视频高度(像素):";
            videoHeightLabel.AutoSize = true; // 允许标签自动调整大小以适应文本
            videoHeightNumeric.Minimum = 240;
            videoHeightNumeric.Maximum = 2160;
            videoHeightNumeric.Value = 1920;
            videoHeightNumeric.DecimalPlaces = 0;
            
            // 使用过渡效果
            useTransitionEffectLabel.Text = "使用过渡效果:";
            useTransitionEffectLabel.AutoSize = true; // 允许标签自动调整大小以适应文本
            useTransitionEffectCheckBox.Checked = false;
            
            // 设置控件位置
            var layoutPanel = new MaterialLabel();
            layoutPanel.Dock = DockStyle.Fill;
            
            var tableLayout = new TableLayoutPanel();
            tableLayout.Dock = DockStyle.Fill;
            tableLayout.ColumnCount = 2;
            tableLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 150));
            tableLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
            tableLayout.RowCount = 6;
            tableLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, 50));
            tableLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, 50));
            tableLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, 50));
            tableLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, 50));
            tableLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, 50));
            tableLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, 50));
            
            // 添加控件到表格布局
            tableLayout.Controls.Add(imageDurationLabel, 0, 0);
            tableLayout.Controls.Add(imageDurationNumeric, 1, 0);
            
            tableLayout.Controls.Add(defaultAudioDurationLabel, 0, 1);
            tableLayout.Controls.Add(defaultAudioDurationNumeric, 1, 1);
            
            tableLayout.Controls.Add(videoWidthLabel, 0, 2);
            tableLayout.Controls.Add(videoWidthNumeric, 1, 2);
            
            tableLayout.Controls.Add(videoHeightLabel, 0, 3);
            tableLayout.Controls.Add(videoHeightNumeric, 1, 3);
            
            tableLayout.Controls.Add(useTransitionEffectLabel, 0, 4);
            tableLayout.Controls.Add(useTransitionEffectCheckBox, 1, 4);
            for (int i = 0; i < tableLayout.Controls.Count; i++)
            {
                var ctrl = tableLayout.Controls[i];
                if (ctrl is MaterialTextBox)
                {
                    ctrl.Width = 400;
                }
                else if (ctrl is NumericUpDown numericUpDown)
                {
                    // 设置NumericUpDown控件的锚定属性
                    numericUpDown.Anchor = AnchorStyles.Left | AnchorStyles.Right;
                }
            }
            layoutPanel.Controls.Add(tableLayout);
            this.Controls.Add(layoutPanel);
        }
        
        // 从配置管理器加载配置数据到控件
        public void LoadConfig()
        {
            var config = ConfigManager.Instance;
            
            imageDurationNumeric.Value = config.GetInt("projectSettings.imageDuration", 3000);
            defaultAudioDurationNumeric.Value = config.GetInt("projectSettings.defaultAudioDuration", 60000);
            videoWidthNumeric.Value = config.GetInt("projectSettings.videoWidth", 1080);
            videoHeightNumeric.Value = config.GetInt("projectSettings.videoHeight", 1920);
            useTransitionEffectCheckBox.Checked = config.GetBool("useTransitionEffect", false);
        }
        
        // 从控件保存配置数据到配置管理器
        public void SaveConfig()
        {
            var config = ConfigManager.Instance;
            
            config.SetValue("projectSettings.imageDuration", (int)imageDurationNumeric.Value);
            config.SetValue("projectSettings.defaultAudioDuration", (int)defaultAudioDurationNumeric.Value);
            config.SetValue("projectSettings.videoWidth", (int)videoWidthNumeric.Value);
            config.SetValue("projectSettings.videoHeight", (int)videoHeightNumeric.Value);
            config.SetValue("useTransitionEffect", useTransitionEffectCheckBox.Checked);
            
            config.SaveConfig();
        }
    }
}